<?php

/**
 * @author Djordje Zeljic
 * Date: Jun 21, 2010 10:34:40 PM
 */
class users_m extends Model {

    var $errors;
    var $gl = 0;

    function users_m () {
        parent::Model();
        $this->load->library('form_validation');
    }// construct of users_m

    function create() {
        if($this->valid()) {

            $data = array(
                    'name' => $this->input->post('name'),
                    'email' => $this->input->post('email'),
                    'password' => md5($this->input->post('password')),
                    'status' => $this->input->post('status'), // 1 for admin and 2 for superadmin
                    'parent' => $this->session->userdata('lgu_user_id'),
                    'date' => time()
            );
            $this->db->insert('users',$data);
            $this->session->set_flashdata('hideform','1');
            return array('success'=>'success');
        }else {
            return array('success'=>'failed','message'=>$this->errors);
        }
    }

    function valid() {
        $this->form_validation->set_rules('name','<b>Name</b>','trim|required');
        $this->form_validation->set_rules('email','<b>eMail</b>','trim|required|valid_email');
        $this->form_validation->set_rules('password','<b>Password</b>','required|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('password_','<b>Re-Password</b>','required|matches[password]');
        $this->form_validation->set_rules('status','Status');

        $numrow = $this->db->where('email',$this->input->post('email'))->get('users')->num_rows();

        if($numrow == 1) {
            $this->form_validation->set_message('required','Desila se greska!');
        }

        if($this->form_validation->run()) {
            return TRUE;
        }else {
            $this->errors = validation_errors(' ','<br />');
            return FALSE;
        }
    }

    function read($id) {
        $row = $this->db->where('id',$id)->get('users')->row_array();
        if($row['parent'] != 0) {
            $row['parent_row'] = $this->db->where('id',$row['parent'])->get('users')->row_array();
        }else {
            $row['parent_row'] = $row;
        }

        return $row;
    }

    function readAll() {
        return $this->db->order_by('id','desc')->get('users')->result_array();
    }

    function log_in() {
        $email = $this->input->post('email');
        $password = md5($this->input->post('password'));
        $row = $this->db->where('email',$email)->where('password',$password)->get('users')->row_array();
        if(!empty($row)) {
            $this->session->set_userdata('lgu_is_log', 1);
            $this->session->set_userdata('lgu_status', $row['status']);
            $this->session->set_userdata('lgu_login_time', time());
            $this->session->set_userdata('lgu_user_id',$row['id']);
            $this->session->set_userdata('lgu_user_name',$row['name']);
            unset($row['password']);// hide password
            return array('success'=>'success','message'=>'Welcome!','row'=>$row);
        }else {
            return array('success'=>'failed','message'=>'Incorrect data!');
        }
    }

    function log_out() {
        $this->session->unset_userdata('lgu_is_log');
        $this->session->unset_userdata('lgu_status');
        $this->session->unset_userdata('lgu_login_time');
        return TRUE;
    }

    function is_log($status = 1) {
        if($this->session->userdata('lgu_is_log') == 1) {

            if($status == $this->session->userdata('lgu_status'))
                return TRUE;
            else
                return FALSE;

        }else {
            return FALSE;
        }
    }

    function check_log($status) {
        if($this->is_log($status)) {
            return TRUE;
        }else {
            //echo "You are not authorized to view this page.";
            redirect(site_url().'rentacar/login');
            exit();
        }
    }

    function delete($id) {
        $this->db->where('id',$id)->delete('users');
        return true;
    }

    function changepass($type) {
        if($this->validatechangepass($type)) {
            $this->db->where('id',$this->input->post('user_id'))->update('users',array('password'=>md5($this->input->post('new_pass'))));
            return array('success'=>'success','message'=>'Password successfully changed!');
        }else {
            return array('success'=>'failed','message'=>$this->errors);
        }
    }

    function validatechangepass($type = 'user') {
        if($type == 'user') {
            $this->form_validation->set_rules('old_pass','<b>Old Password</b>','required|callback_checkoldpass');
        }
        $this->form_validation->set_rules('new_pass','<b>New Password</b>','required|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('renew_pass','<b>Re-New Password</b>','required|matches[new_pass]');
        if($this->form_validation->run()) {
            return TRUE;
        }else {
            $this->errors = validation_errors('', '<br />');
            return FALSE;
        }
    }

    function checkoldpass() {
        $row = $this->db->where('id',$this->input->post('user_id'))->get('users')->row_array();
        if($row['password'] != md5($this->input->post('old_pass'))) {
            $this->form_validation->set_message('checkoldpass', 'Wrong %s');
            return FALSE;
        }else {
            return TRUE;
        }
    }

}
?>